package com.lipi.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lipi.mall.product.entity.AttrEntity;
import com.lipi.mall.product.service.AttrAttrgroupRelationService;
import com.lipi.mall.product.service.AttrService;
import com.lipi.mall.product.service.CategoryService;
import com.lipi.mall.product.vo.AttrGroupRelationVO;
import com.lipi.mall.product.vo.AttrGroupWithAttrsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lipi.mall.product.entity.AttrGroupEntity;
import com.lipi.mall.product.service.AttrGroupService;
import com.lipi.common.utils.PageUtils;
import com.lipi.common.utils.R;


/**
 * 属性分组
 *
 * @author hecy
 * @email kuanih200@163.com
 * @date 2021-06-14 16:02:24
 */
@RestController
@Slf4j
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        //1、查出当前分类的所有属性分组
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVO> data = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", data);
    }

    // /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> vos) {
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    // product/attrgroup/{attrgroupId}/attr/relation
    // 获取分组下关联的所有基本属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entityList);
    }


    // /product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 查询没有被关联的属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrnoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (attrGroup == null) {
            return R.error();
        }
        Long catelogId = attrGroup.getCatelogId();

        Long[] categoryPath = categoryService.getCategoryPath(catelogId);

        attrGroup.setCatelogPath(categoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
